// firstPackage/pages/secondReferencePrice/secondReferencePrice.js


import { loginWxAction } from "../../../service/config"
import { bindUserRelation } from "../../../service/user_service"
import { queryAllExpertList } from "../../../service/expert_service"
import debounce from "../../../tools/debounce"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    isFoldArea: false,
    isFoldPrice: false,
    dataList: [

    ],
    page: 0,
    pageSize: 30,
    searchWords: "",
    areaName: "",
    minPirce: "",
    maxPrice: "",
    singlePopType: "",
    showSinglePop: false,
    singlePopTitle: "",
    scrollRefreshStatus: true,
    isShowLoading: false,
    isBottomShow: false,
    isShare: false,
    bindUserId: "",

    expertList: [

    ],
    expertPage: 0,
    expertPageSize: 5,
    showExpertPop: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isShare) {
      this.setData({
        isShare: true,
        bindUserId: options.userId || ""
      })
      //wx-openId登录
      this.userWxLogin();
    } else {
     
      this.queryAllExpertList();
    }

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //用户登陆授权等:
  userWxLogin() {
    loginWxAction().then((res => {
      console.log("JJLog:登录返回:" + JSON.stringify(res));
      let data = res.data.data;

      if (res.code == 200) {
        app.globalData.nickName = data.nickName;
        app.globalData.avatar = data.avatar;
        app.globalData.name = data.name || "";
        app.globalData.phone = data.phone || "";
        app.globalData.isLogin = data.isLogin;
        app.globalData.userId = data.id;
        app.globalData.qrCode = data.qrCode || "";
        app.globalData.isAuthUserInfo = data.isAuthUserInfo;
        app.globalData.weChat = data.weChat || "";
        app.globalData.roleName = data.roleName || "";
        app.globalData.companyName = data.companyName || "";
        app.globalData.companyNumber = data.companyNumber || "";
        app.globalData.isBindMp = data.isBindMp;
        app.globalData.roleType = data.roleType || 1
        this.setData({
          roleType: app.globalData.roleType
        })
        //绑定挂级关系:
        if (this.data.bindUserId) {
          this.bindUserRelation();
        }

      
        this.queryAllExpertList();

      }
    })).catch(err => {
      console.error("JJLog:userWxLogin", err);

    })
  },
  //查询所有专家:
  queryAllExpertList() {
    queryAllExpertList(this.data.expertPage, this.data.expertPageSize, "").then(res => {
      console.log("JJLog:所有专家是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          expertList: res.data || []
        })
      }
    }).catch(err => {
      console.error("JJLog:queryAllExpertList:", err);
    })
  },

  //绑定挂级关系:
  bindUserRelation() {
    let type = 2;
    bindUserRelation(type, this.data.bindUserId).then(res => {
      console.log("JJLog:绑定挂级关系:" + JSON.stringify(res));
    }).catch(err => {
      console.error("JJLog:bindUserRelation:", err);
    })
  },

  searchAction(e) {
    console.log("JJLog:搜索数据:" + JSON.stringify(e));
    let words = e.detail.searchWord;
    this.setData({
      page: 0,
      isBottomShow: false,
      searchWords: words
    })
   

  },

  



  //单项选择切换:
  singlePopAction(e) {
    let type = e.currentTarget.dataset.type;
    if (type == "adArea") {
      this.setData({
        singlePopType: type,
        showSinglePop: true,
        singlePopTitle: "行政区域"
      })
    } else if (type == "adPrice") {
      this.setData({
        singlePopType: type,
        showSinglePop: true,
        singlePopTitle: "参考价格"
      })
    }


  },

  //单项选择回调:
  singlePopSelectCallback(e) {
    console.log("JJLog:单项选择回调:" + JSON.stringify(e));
    let data = e.detail;
    let result = data.result;
    let type = data.type;
    if (type == "adArea") {
      this.setData({
        page: 0,
        isBottomShow: false,
        areaName: result.value || "",
        showSinglePop: false
      })
    }
    if (type == "adPrice") {
      if (result.value) {
        let spArr = result.value.split("-");
        this.setData({
          page: 0,
          isBottomShow: false,
          minPirce: spArr[0],
          maxPrice: spArr[1],
          showSinglePop: false
        })
      } else {
        this.setData({
          page: 0,
          isBottomShow: false,
          minPirce: "",
          maxPrice: "",
          showSinglePop: false
        })
      }

    }

   

  },

  singlePopHide() {
    this.setData({
      showSinglePop: false
    })
  },


  //下拉刷新数据:
  refreshDownData() {
    console.log("JJLog:下拉刷新来了=====:");
    this.setData({
      scrollRefreshStatus: true,
      page: 0,
      areaName: "",
      minPirce: "",
      maxPrice: "",
      isBottomShow: false,
    })
  
  },

  //滚动到底了
  scrolltolower() {
    if (this.data.isBottomShow) {
      //已经到底了就不加载了
      return;
    }
    let tempPage = this.data.page + 1;
    this.setData({
      page: tempPage,
      isShowLoading: true,
    })
   
  },

  backHomeAction() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },

  //咨询专家点击:
  askExpertAction() {
    
    if (this.data.expertList && this.data.expertList.length < 1) {
      wx.showToast({
        title: "暂时无法咨询",
        icon: "none"
      })
      return;
    }
    this.setData({
      showExpertPop: true
    })

  },
  exportPopHidden() {
    this.setData({
      showExpertPop: false
    })
  },

  exportPhoneClick(e) {
    console.log("JJLog:电话:" + JSON.stringify(e));
    let item = e.detail.item;
    this.commonMakePhone(item);
  },
  //公共的打电话
  commonMakePhone(item) {
    if (item.phone) {
      wx.makePhoneCall({
        phoneNumber: item.phone,
        fail: err => {

        }
      })
      this.setData({
        showExpertPop: false
      })

    } else {
      wx.showToast({
        title: '暂无电话',
        icon: "none"
      })
      this.setData({
        showExpertPop: false
      })
    }
  },
  commonExpertChatAction(item) {
    this.setData({
      showExpertPop: false,
    })
    let name = "";
    if (item.realName) {
      name = item.realName;
    } else {
      name = item.name
    }
    let chatData = {
      name: name,
      avatar: item.avatar,
      chatUserId: item.openId,
      conversationType: "C2C",
      houseNumber: "",
      houseName: "",
      expertId: item.id,
      expertPhone: item.phone || "",
      expertWeChat: item.wxChat || "",
    }
    app.globalData.chatData = chatData;
    wx.navigateTo({
      url: '/firstPackage/pages/msgModule/chatRoom/chatRoom',
    })
  },

  exportMsgClick(e) {
    console.log("JJLog:聊天:" + JSON.stringify(e));
    let item = e.detail.item;
    if (!app.globalData.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      this.setData({
        showExpertPop: false
      })
      return;
    }
    this.commonExpertChatAction(item);
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = app.globalData.userId || "";
    let path = "/firstPackage/pages/secondReferencePrice/secondReferencePrice?isShare=1" + "&userId=" + userId
    return {
      title: app.globalData.commonShareTitle,
      path: path
    }
  }

})



